<template>
  <div class="app-container" style="padding: 20px 0">
    <el-row :gutter="20" style="padding: 0 0 20px 0">
        <el-col :span="8">
            <el-input
              v-model="queryInfo.name"
              placeholder="请输入分类名"
              clearable
              @change="handleQuery"
            />
        </el-col>
        <el-col :span="4">
            <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
        </el-col>
    </el-row>

    

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" stripe :data="categoryList" @selection-change="handleSelectionChange" style="margin-top: 10px">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" width="55" prop="id" />
      <el-table-column label="分类名" align="center" prop="name" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :page-size.sync="queryInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[5, 10, 20, 30]"
      :current-page.sync="queryInfo.page"
      @current-change="getList"
      @size-change="getList"
    />

    <!-- 添加或修改分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分类名" prop="name" required>
          <el-input v-model="form.name" placeholder="请输入分类名" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Category',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分类表格数据
      categoryList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryInfo: {
          name: '',
          page: 1,
          pageSize: 5
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{required: true, message: '请输入分类名', trigger: "blur"}]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询分类列表 */
    getList() {
      this.loading = true
      const queryInfo = this.queryInfo
      console.log(queryInfo);
      this.$http.get('/category/list', {
        params: {
          ...queryInfo
        }
      }).then(res => {
        console.log(res);
        if (!res.data.status) {
            this.$message.error(res.data.message)
        }
        this.categoryList = res.data.data.records
        this.total = res.data.data.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        // pid: null,
        description: null,
      }
      // this.$refs.queryForm.resetFields()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryInfo.page = 1
      console.log(this.queryInfo);
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm('queryForm')
      this.$refs.queryForm.resetFields()
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加分类'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.open = true
      this.title = '修改分类'
      this.form = {
        id: row.id,
        name: row.name,
        description: row.description
      }
      // const id = row.id
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.$http.put('/category', this.form).then(response => {
              this.$message.success('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            this.$http.post('/category', this.form).then(response => {
              this.$message.success('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const ids = row.id || this.ids
      const confirmResult = await this.$confirm('是否要删除此分类，此分类下的文章也会一并删除？, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch(err => err)
      if (confirmResult !== 'confirm') {
          return this.$message.info('已取消删除！')
      }
      const {data: res} = await this.$http.delete(`/category`, {
          params: {
              ids: ids + ''
          }
      })

      if (res.status) {
        this.getList()
      }
      this.$message(
        {
          type: res.status ? 'success' : 'error',
          message: res.message
        }
      )
    }
  }
}
</script>